<template>
    <div>
        <div>
            <el-form ref="searchForm" :inline="true" :model="condition.search" label-suffix="" label-width="100px"
                     label-position="left" size="small">
                <el-row>
                    <el-col :span=6>
                        <el-form-item label="门店" prop="shopId">
                            <el-select v-model="condition.search.shopId" placeholder="请选择门店" style="width:200px;">
                                <el-option
                                    v-for="item in shops"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.id">
                                    {{ item.name }}
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span=6>
                        <el-form-item label="手机号" prop="mobileNo">
                            <el-input v-model="condition.search.mobileNo" placeholder="手机号"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span=6>
                        <el-form-item label="称呼" prop="nickname">
                            <el-input v-model="condition.search.nickname" placeholder="称呼"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span=6>
                        <el-form-item label="年龄" prop="age">
                            <el-select v-model="condition.search.age" placeholder="年龄">
                                <el-option
                                    v-for="item in ageOptions"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span=6>
                        <el-form-item label="客户级别" prop="level">
                            <el-select v-model="condition.search.level"
                                       placeholder="客户级别">
                                <el-option
                                    v-for="item in levelOptions"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span=6>
                        <el-form-item label="职业" prop="position">
                            <el-select v-model="condition.search.position"
                                       placeholder="职业">
                                <el-option
                                    v-for="item in positionOptions"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col>
                        <el-form-item>
                            <el-button type="primary" @click="getData">查询</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="showAddDialog">新增</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="reset">重置</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="exportExcel">导出</el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </div>

        <el-table
            id="mainTable"
            size="small"
            :data="tableData"
            header-cell-class-name="tableHeaderStyle"
            :row-class-name="tableRowClassName">
            <el-table-column label="序号" width="50">
                <template v-slot="scope">
                    <span>{{ (condition.page - 1) * condition.rows + scope.$index + 1 }}</span>
                </template>
            </el-table-column>
            <el-table-column
                prop="mobileNo"
                label="手机号">
            </el-table-column>
            <el-table-column
                prop="shopName"
                label="门店">
            </el-table-column>
            <el-table-column
                prop="nickname"
                label="称呼">
            </el-table-column>
            <el-table-column
                prop="level"
                label="客户等级">
                <template v-slot="scope">
                    <el-tag type="success" effect="dark" size="small"
                            v-if="scope.row.level == 'CORE' || scope.row.level == '核心'">
                        {{ formatLevel(scope.row, scope.column, scope.row.level) }}
                    </el-tag>
                    <el-tag type="" effect="dark" size="small"
                            v-if="scope.row.level == 'IMPORTANT' || scope.row.level == '重要'">
                        {{ formatLevel(scope.row, scope.column, scope.row.level) }}
                    </el-tag>
                    <el-tag type="info" size="small" effect="dark"
                            v-if="scope.row.level == 'GENERAL' || scope.row.level == '一般'">
                        {{ formatLevel(scope.row, scope.column, scope.row.level) }}
                    </el-tag>
                </template>
            </el-table-column>
            <el-table-column
                prop="age"
                :formatter="formatAge"
                label="年龄">
            </el-table-column>
            <el-table-column
                prop="position"
                :formatter="formatPosition"
                label="职业">
            </el-table-column>
            <el-table-column
                prop="gender"
                :formatter="formatGender"
                label="性别">
            </el-table-column>
            <el-table-column
                prop="birthday"
                label="生日">
            </el-table-column>
            <el-table-column
                prop="flavour"
                label="顾客口味">
            </el-table-column>
            <el-table-column
                prop="dinnerCount"
                label="用餐次数">
            </el-table-column>
            <el-table-column
                prop="remark"
                label="备注">
                <template v-slot="scope">
                    <table-popover :text=scope.row.remark :length="16"></table-popover>
                </template>
            </el-table-column>
            <el-table-column
                prop="source"
                :formatter="formatSource"
                label="来源">
            </el-table-column>
            <el-table-column
                prop="createTime"
                label="创建时间">
            </el-table-column>
            <el-table-column
                label="操作"
                fixed="right"
                align="center"
                width="120px">
                <template v-slot="scope">
                    <div>
                        <el-button @click="onDetail(scope.row)" type="text" size="small">详情</el-button>
                        <el-button @click="showUpdateDialog(scope.row)" type="text" size="small">编辑</el-button>
                        <el-button @click="onDelete(scope.row)" type="text" size="small">删除</el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
            background
            small
            :hide-on-single-page="true"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page.sync="condition.page"
            :page-sizes="[10, 20, 30, 40, 50, 100]"
            :page-size.sync="condition.rows"
            layout="sizes, prev, pager, next"
            :total.sync="condition.total">
        </el-pagination>
        <el-drawer ref="drawer" direction="rtl" :title=addOrUpdateDialogTitle width="30%"
                   :visible.sync="addOrUpdateDialogVisible">
            <el-form :model="addOrUpdateForm" label-width="120px" size="small">
                <el-form-item label="选择门店">
                    <el-select v-model="addOrUpdateForm.shopId" placeholder="请选择门店" style="width:200px;">
                        <el-option
                            v-for="item in shops"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                            {{ item.name }}
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="手机号">
                    <el-input v-model="addOrUpdateForm.mobileNo" placeholder="手机号"></el-input>
                </el-form-item>
                <el-form-item label="称呼">
                    <el-input v-model="addOrUpdateForm.nickname" placeholder="称呼"></el-input>
                </el-form-item>
                <el-form-item label="生日">
                    <el-date-picker
                        v-model="addOrUpdateForm.birthday"
                        type="date"
                        placeholder="生日"
                        value-format="yyyy-MM-dd"
                        align="right">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="年龄">
                    <el-select v-model="addOrUpdateForm.age"
                               placeholder="年龄">
                        <el-option
                            v-for="item in ageOptions"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="级别">
                    <el-select v-model="addOrUpdateForm.level"
                               placeholder="级别">
                        <el-option
                            v-for="item in levelOptions"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="职业">
                    <el-select v-model="addOrUpdateForm.position"
                               placeholder="职业">
                        <el-option
                            v-for="item in positionOptions"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="性别">
                    <el-select v-model="addOrUpdateForm.gender"
                               placeholder="性别">
                        <el-option
                            v-for="item in genderOptions"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="个人口味">
                    <el-input v-model="addOrUpdateForm.flavour" placeholder="个人口味" type="textarea"></el-input>
                </el-form-item>
                <el-form-item label="备注">
                    <el-input v-model="addOrUpdateForm.remark" placeholder="备注" type="textarea"></el-input>
                </el-form-item>
            </el-form>
            <div class="drawer-footer">
                <el-button class="drawer-footer-button" @click="addOrUpdateDialogVisible = false" size="small">取 消
                </el-button>
                <el-button class="drawer-footer-button" @click="addOrUpdateConfirm" type="primary" size="small">确 定
                </el-button>
            </div>
        </el-drawer>
    </div>
</template>

<script>
import {addCustomer, deleteCustomer, getCustomerByPage, getShopByPage, updateCustomer} from '@/api/api'
import TablePopover from "@/component/single/TablePopover";

export default {
    name: "Customer",
    components: {TablePopover},
    data() {
        return {
            // 分页搜索条件
            condition: {
                page: 1,
                rows: 10,
                total: 0,
                search: {}
            },
            // 表格数据
            tableData: [],
            // 新增或者编辑对话框显示
            addOrUpdateDialogVisible: false,
            // 标题
            addOrUpdateDialogTitle: '新增',
            // 新增或者编辑对话框表单
            addOrUpdateForm: {},
            // 选中行
            selectedRow: {},
            // 门店数据
            shops: [],

            ageOptions: this.CONST.age_options,
            levelOptions: this.CONST.level_options,
            positionOptions: this.CONST.position_options,
            genderOptions: this.CONST.gender_options,
        }
    },
    // 这里定义需要父组件传过来的东西
    props: {
        size: {
            type: String,
            default: 'small'
        },
        shopId: String,
    },
    methods: {
        // 这里可以单独设置每一行的样式
        tableRowClassName({row, rowIndex}) {
            return '';
        },
        handleSizeChange(val) {
            this.getData();
        },
        handleCurrentChange(val) {
            this.getData();
        },
        // 格式布尔值
        formatBoolean: function (row, column, cellValue) {
            if (cellValue) {
                return "是";
            } else {
                return "否";
            }
        },
        formatSource: function (row, column, cellValue) {
            if (cellValue == 'MANUAL') {
                return "手工添加";
            } else if (cellValue == 'MANUAL_BY_MNP') {
                return "小程序添加";
            } else if (cellValue == 'DP_RESERVE') {
                return "点评预定";
            } else if (cellValue == 'RESERVE_UPLOAD') {
                return "预定上传";
            } else if (cellValue == 'RESERVE_NOTICE') {
                return "预定通知";
            } else {
                return "未知";
            }
        },
        formatAge: function (row, column, cellValue) {
            if (!cellValue) {
                return;
            }
            for (const elem of this.ageOptions) {
                if (elem.value == cellValue) {
                    return elem.label;
                }
            }
            return "未知";
        },
        formatLevel: function (row, column, cellValue) {
            if (!cellValue) {
                return;
            }
            for (const elem of this.levelOptions) {
                if (elem.value == cellValue) {
                    return elem.label;
                }
            }
            return "未知";
        },
        formatPosition: function (row, column, cellValue) {
            if (!cellValue) {
                return;
            }
            for (const elem of this.positionOptions) {
                if (elem.value == cellValue) {
                    return elem.label;
                }
            }
            return "未知";
        },
        formatGender: function (row, column, cellValue) {
            if (!cellValue) {
                return;
            }
            for (const elem of this.genderOptions) {
                if (elem.value == cellValue) {
                    return elem.label;
                }
            }
            return "未知";
        },
        showAddDialog: function () {
            this.addOrUpdateDialogTitle = '新增';
            this.addOrUpdateDialogVisible = true;
            this.addOrUpdateForm = {};
        },
        showUpdateDialog: function (row) {
            this.addOrUpdateDialogTitle = '编辑';
            this.addOrUpdateDialogVisible = true;
            this.selectedRow = row;
            // 深拷贝
            this.addOrUpdateForm = Object.assign({}, row);
        },
        reset: function () {
            this.$refs['searchForm'].resetFields();
        },
        exportExcel: function () {
            this.GLOBAL.exportExcel('#mainTable', '数据.xlsx');
        },
        add: function () {
            addCustomer(this.addOrUpdateForm).then(data => {
                if (data.respCode == 1000) {
                    this.addOrUpdateDialogVisible = false;
                    this.getData();
                }
            });
        },
        update: function () {
            updateCustomer(this.addOrUpdateForm).then(data => {
                if (data.respCode == 1000) {
                    this.addOrUpdateDialogVisible = false;
                    this.getData();
                }
            });
        },
        onDetail: function (row) {
            this.$router.push({path: '/merchant/shop/customer/detail', query: {mobileNo: row.mobileNo}});
        },
        onDelete: function (row) {
            this.$confirm('请确认是否删除, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteCustomer({id: row.id}).then(data => {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.getData();
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消'
                });
            });
        },
        addOrUpdateConfirm: function () {
            if (this.addOrUpdateDialogTitle == '新增') {
                this.add();
            } else if (this.addOrUpdateDialogTitle == '编辑') {
                this.update();
            }
        },
        // 获取门店列表
        getShops: function () {
            let condition = {
                page: 1,
                rows: 100,
                search: {}
            };
            getShopByPage(condition).then(data => {
                this.shops = data.body.rows;
            });
        },
        // 获取表格数据
        getData: function () {
            if (this.shopId) {
                this.condition.search.shopId = this.shopId;
            }
            getCustomerByPage(this.condition).then(data => {
                this.tableData = data.body.rows;
                this.condition.total = data.body.total;
            });
            this.getShops();
        },
    },
    created() {
        this.getData();
    },
}
</script>

<style scoped>
.el-input {
    width: 150px !important;
}

.el-select {
    width: 150px !important;
}
</style>
